package com.qfedu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qfedu.entity.OrderEntity;
import com.qfedu.service.OrderService;
import com.qfedu.util.Ajaxd;
import com.qfedu.util.Page;
import com.qfedu.util.SaleDate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.time.Year;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
public class StatisticsController {
@Reference(check = false)
    private OrderService orderService;

@RequestMapping("/orderstatistics.to")
    public String toStatistics(HttpServletRequest request){
    Date date = new Date();
    Calendar instance = Calendar.getInstance();
    instance.setTime(date);
    int year = instance.get(Calendar.YEAR);
    request.setAttribute("year",year);
    return "charts-main";
}

@RequestMapping("/orderstatistics.do")
@ResponseBody
    public Page doorderstatistics(int pageNum, @RequestParam(value = "pageSize",required = false,defaultValue = "2") int pageSize){
//    List<OrderEntity> orderEntities = orderService.queryAllOrder();
    Page page = new Page(pageNum,pageSize);
     page = orderService.queryStatisPage(pageNum,pageSize);
    List<Integer> data=new ArrayList<>(0);
    String xqj="";
    for (int i = 0; i <7 ; i++) {
        data.add(0);
    }
    for(OrderEntity orderEntity:page.getOrderlist()){
     xqj=getweek(orderEntity.getCreateTime());
     switch (xqj){
         case "星期一":{
              data.set(0,data.get(0)+1);
              break;
         }
         case "星期二":{
             data.set(1,data.get(1)+1);
             break;
         }
         case "星期三":{
             data.set(2,data.get(2)+1);
             break;
         }
         case "星期四":{
             data.set(3,data.get(3)+1);
             break;
         }
         case "星期五":{
             data.set(4,data.get(4)+1);
             break;
         }
         case "星期六":{
             data.set(5,data.get(5)+1);
             break;
         }
         case "星期日":{
             data.set(6,data.get(6)+1);
             break;
         }
     }
    }
    page.setSaledata(data);

    return page;
}


public String getweek(Date date){
    String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
    if(week_index<0) {
        week_index = 0;
    }
    return weeks[week_index];
}

@RequestMapping("/statisticByMonth.do")
@ResponseBody
    public List<Integer> domonth(int year){
    List<OrderEntity> orderEntities = orderService.queryStatisYear(year);
    List<Integer> data=new ArrayList<>();
    for (int i = 0; i <12 ; i++) {
        data.add(0);
    }
    for (OrderEntity orderEntity:orderEntities){
        data.set(orderEntity.getOmonth(),data.get(orderEntity.getOmonth())+1);
    }

    return data;
}


@RequestMapping("/incomeStatis.to")
    public String toincomestatis(HttpServletRequest request){
    Date date = new Date();
    Calendar instance = Calendar.getInstance();
    instance.setTime(date);
    int year = instance.get(Calendar.YEAR);
    request.setAttribute("year",year);
    return "statis-main";
}

@RequestMapping("/incomestatis.do")
@ResponseBody
    public Ajaxd doincomestatis(int year){
    Ajaxd ajaxd = new Ajaxd();
    List<String> list=new ArrayList<>();
    list.add(String.valueOf(year));
    list.add(String.valueOf(year-1));
    list.add(String.valueOf(year-2));
    List<Double> list1 = new ArrayList<>();
    List<Double> list2 = new ArrayList<>();
    List<Double> list3 = new ArrayList<>();
    for (int i = 0; i <12 ; i++) {
      list1.add(0.0);
      list2.add(0.0);
      list3.add(0.0);
    }
    getlist(year, list1);
    getlist(year-1,list2);
    getlist(year-2,list3);
    ajaxd.setYdata(list);
    ajaxd.setList1(list1);
    ajaxd.setList2(list2);
    ajaxd.setList3(list3);
    return ajaxd;

}

    private void getlist(int year, List<Double> list1) {
      List<OrderEntity> orderEntities = orderService.queryStatisYear(year);
        for (OrderEntity orderEntity:orderEntities){
           list1.set(orderEntity.getOmonth(),list1.get(orderEntity.getOmonth())+orderEntity.getOprice()*0.2);
       }
    }

}
